﻿using System;
using System.Collections.Generic;

public class MessageManager
{
    static MessageManager m_Instance;
    public static MessageManager Instance
    {
        get
        {
            if (m_Instance == null)
            {
                m_Instance = new MessageManager();
            }
            return m_Instance;
        }
    }

    Dictionary<string, Action> m_Listeners = new Dictionary<string, Action>();

    public void AddListener(string messageType, Action callback)
    {
        if (m_Listeners.TryGetValue(messageType, out Action listeners))
        {
            listeners += callback;
            m_Listeners[messageType] = listeners;
        }
        else
        {
            m_Listeners.Add(messageType, callback);
        }
    }
    public void RemoveListener(string messageType, Action callback)
    {
        if (m_Listeners.TryGetValue(messageType, out Action listeners))
        {
            listeners -= callback;
        }
    }
    public void Invoke(string messageType)
    {
        if (m_Listeners.TryGetValue(messageType, out Action listeners))
        {
            listeners?.Invoke();
        }
    }
}

public class MessageManager<T>
{
    static MessageManager<T> m_Instance;
    public static MessageManager<T> Instance
    {
        get
        {
            if (m_Instance == null)
            {
                m_Instance = new MessageManager<T>();
            }
            return m_Instance;
        }
    }

    Dictionary<string, Action<T>> m_Listeners = new Dictionary<string, Action<T>>();

    public void AddListener(string messageType, Action<T> callback)
    {
        if (m_Listeners.TryGetValue(messageType, out Action<T> listeners))
        {
            listeners += callback;
            m_Listeners[messageType] = listeners;
        }
        else
        {
            m_Listeners.Add(messageType, callback);
        }
    }
    public void RemoveListener(string messageType, Action<T> callback) 
    {
        if (m_Listeners.TryGetValue(messageType, out Action<T> listeners))
        {
            listeners -= callback;
        }
    }
    public void Invoke(string messageType, T param)
    {
        if (m_Listeners.TryGetValue(messageType, out Action<T> listeners))
        {
            listeners?.Invoke(param);
        }
    }
}

public class MessageManager<T1, T2>
{
    static MessageManager<T1, T2> m_Instance;
    public static MessageManager<T1, T2> Instance
    {
        get
        {
            if (m_Instance == null)
            {
                m_Instance = new MessageManager<T1, T2>();
            }
            return m_Instance;
        }
    }

    Dictionary<string, Action<T1, T2>> m_Listeners = new Dictionary<string, Action<T1, T2>>();

    public void AddListener(string messageType, Action<T1, T2> callback)
    {
        if (m_Listeners.TryGetValue(messageType, out Action<T1, T2> listeners))
        {
            listeners += callback;
            m_Listeners[messageType] = listeners;
        }
        else
        {
            m_Listeners.Add(messageType, callback);
        }
    }
    public void RemoveListener(string messageType, Action<T1, T2> callback)
    {
        if (m_Listeners.TryGetValue(messageType, out Action<T1, T2> listeners))
        {
            listeners -= callback;
        }
    }
    public void Invoke(string messageType, T1 param1, T2 param2)
    {
        if (m_Listeners.TryGetValue(messageType, out Action<T1, T2> listeners))
        {
            listeners?.Invoke(param1, param2);
        }
    }
}

public class MessageManager<T1, T2, T3>
{
    static MessageManager<T1, T2, T3> m_Instance;
    public static MessageManager<T1, T2, T3> Instance
    {
        get
        {
            if (m_Instance == null)
            {
                m_Instance = new MessageManager<T1, T2, T3>();
            }
            return m_Instance;
        }
    }

    Dictionary<string, Action<T1, T2, T3>> m_Listeners = new Dictionary<string, Action<T1, T2, T3>>();

    public void AddListener(string messageType, Action<T1, T2, T3> callback)
    {
        if (m_Listeners.TryGetValue(messageType, out Action<T1, T2, T3> listeners))
        {
            listeners += callback;
            m_Listeners[messageType] = listeners;
        }
        else
        {
            m_Listeners.Add(messageType, callback);
        }
    }
    public void RemoveListener(string messageType, Action<T1, T2, T3> callback)
    {
        if (m_Listeners.TryGetValue(messageType, out Action<T1, T2, T3> listeners))
        {
            listeners -= callback;
        }
    }
    public void Invoke(string messageType, T1 param1, T2 param2, T3 param3)
    {
        if (m_Listeners.TryGetValue(messageType, out Action<T1, T2, T3> listeners))
        {
            listeners?.Invoke(param1, param2, param3);
        }
    }
}